{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Find Positive Integer Solution for a Given Equation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #math #two-pointers #binary-search #interactive"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数学 #双指针 #二分查找 #交互"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: findSolution"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #找出给定方程的正整数解"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个函数  <code>f(x, y)</code> 和一个目标结果 <code>z</code>，函数公式未知，请你计算方程 <code>f(x,y) == z</code> 所有可能的正整数 <strong>数对</strong> <code>x</code> 和 <code>y</code>。满足条件的结果数对可以按任意顺序返回。</p>\n",
    "\n",
    "<p>尽管函数的具体式子未知，但它是单调递增函数，也就是说：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>f(x, y) < f(x + 1, y)</code></li>\n",
    "\t<li><code>f(x, y) < f(x, y + 1)</code></li>\n",
    "</ul>\n",
    "\n",
    "<p>函数接口定义如下：</p>\n",
    "\n",
    "<pre>\n",
    "interface CustomFunction {\n",
    "public:\n",
    "  // Returns some positive integer f(x, y) for two positive integers x and y based on a formula.\n",
    "  int f(int x, int y);\n",
    "};</pre>\n",
    "\n",
    "<p>你的解决方案将按如下规则进行评判：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>判题程序有一个由 <code>CustomFunction</code> 的 <code>9</code> 种实现组成的列表，以及一种为特定的 <code>z</code> 生成所有有效数对的答案的方法。</li>\n",
    "\t<li>判题程序接受两个输入：<code>function_id</code>（决定使用哪种实现测试你的代码）以及目标结果 <code>z</code> 。</li>\n",
    "\t<li>判题程序将会调用你实现的 <code>findSolution</code> 并将你的结果与答案进行比较。</li>\n",
    "\t<li>如果你的结果与答案相符，那么解决方案将被视作正确答案，即 <code>Accepted</code> 。</li>\n",
    "</ul>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>function_id = 1, z = 5\n",
    "<strong>输出：</strong>[[1,4],[2,3],[3,2],[4,1]]\n",
    "<strong>解释：</strong>function_id = 1 暗含的函数式子为 f(x, y) = x + y\n",
    "以下 x 和 y 满足 f(x, y) 等于 5：\n",
    "x=1, y=4 -> f(1, 4) = 1 + 4 = 5\n",
    "x=2, y=3 -> f(2, 3) = 2 + 3 = 5\n",
    "x=3, y=2 -> f(3, 2) = 3 + 2 = 5\n",
    "x=4, y=1 -> f(4, 1) = 4 + 1 = 5\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>function_id = 2, z = 5\n",
    "<strong>输出：</strong>[[1,5],[5,1]]\n",
    "<strong>解释：</strong>function_id = 2 暗含的函数式子为 f(x, y) = x * y\n",
    "以下 x 和 y 满足 f(x, y) 等于 5：\n",
    "x=1, y=5 -> f(1, 5) = 1 * 5 = 5\n",
    "x=5, y=1 -> f(5, 1) = 5 * 1 = 5</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 <= function_id <= 9</code></li>\n",
    "\t<li><code>1 <= z <= 100</code></li>\n",
    "\t<li>题目保证 <code>f(x, y) == z</code> 的解处于 <code>1 <= x, y <= 1000</code> 的范围内。</li>\n",
    "\t<li>在 <code>1 <= x, y <= 1000</code> 的前提下，题目保证 <code>f(x, y)</code> 是一个 32 位有符号整数。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [find-positive-integer-solution-for-a-given-equation](https://leetcode.cn/problems/find-positive-integer-solution-for-a-given-equation/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [find-positive-integer-solution-for-a-given-equation](https://leetcode.cn/problems/find-positive-integer-solution-for-a-given-equation/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['1\\n5', '2\\n5']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        ans = list()\n",
    "        y = 1000\n",
    "        for x in range(1,1001):\n",
    "            while y and customfunction.f(x,y)>z:\n",
    "                y-=1\n",
    "            if y==0:\n",
    "                break\n",
    "            if customfunction.f(x,y)==z:\n",
    "                ans.append([x,y])\n",
    "        return ans\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        x = 1\n",
    "        y = 1000\n",
    "        result = []\n",
    "        while x<1000 and y>0:\n",
    "            if customfunction.f(x,y) < z:\n",
    "                x += 1\n",
    "            elif customfunction.f(x,y) > z:\n",
    "                y -= 1\n",
    "            else:\n",
    "                result.append([x,y])\n",
    "                x += 1\n",
    "                # y -= 1\n",
    "        return result\n",
    "        \n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        nums = list(range(1, z + 1))\n",
    "        ans = []\n",
    "        l = 1\n",
    "        r = 1000\n",
    "        while l <= 1000 and  r > 0:\n",
    "            x = customfunction.f(l, r)\n",
    "            if x == z:\n",
    "                ans.append([l, r])\n",
    "                l += 1\n",
    "                r -= 1\n",
    "            elif x > z:\n",
    "                r -= 1\n",
    "            else:\n",
    "                l += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        ans = []\n",
    "        x = 1\n",
    "        while customfunction.f(x,1) < z + 1:\n",
    "            y = 1\n",
    "            while customfunction.f(x, y) < z:\n",
    "                y += 1\n",
    "            if customfunction.f(x, y) == z:\n",
    "                ans.append([x,y])\n",
    "            x += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        ans = []\n",
    "        y = 1000\n",
    "        for x in range(1, 1001):\n",
    "            while y and customfunction.f(x, y) > z:\n",
    "                y -= 1\n",
    "            if y == 0:\n",
    "                break\n",
    "            if customfunction.f(x, y) == z:\n",
    "                ans.append([x, y])\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        ans = []\n",
    "        last_y = z + 1\n",
    "        for x in range(1, z+1):\n",
    "            y = 1 + bisect_left(range(1, last_y), z, key=partial(customfunction.f, x))\n",
    "            if customfunction.f(x, y) == z:\n",
    "                ans.append([x, y])\n",
    "                last_y = y\n",
    "\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        ans = []\n",
    "        last_y = z + 1\n",
    "        for x in range(1, z+1):\n",
    "            y = 1 + bisect_left(range(1, last_y), z, key=lambda y: customfunction.f(x, y))\n",
    "            if customfunction.f(x, y) == z:\n",
    "                ans.append([x, y])\n",
    "                last_y = y\n",
    "\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        func = customfunction.f\n",
    "        ans = []\n",
    "        x, y = 1, 1000\n",
    "        while x<=1000 and y>=1:\n",
    "            f = func(x, y)\n",
    "            if f == z:\n",
    "                ans.append([x, y])\n",
    "                y -= 1\n",
    "                x += 1\n",
    "            elif f > z:\n",
    "                y -= 1\n",
    "            else:\n",
    "                x += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        ans = []\n",
    "        for x in range(1, 1001):\n",
    "            y = 1 + bisect_left(range(1, 1000), z, key=lambda y: customfunction.f(x, y))\n",
    "            if customfunction.f(x, y) == z:\n",
    "                ans.append([x, y])\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        ans = []\n",
    "        last_y = z + 1\n",
    "        for x in range(1, z+1):\n",
    "            # y = 1 + bisect_left(range(1, last_y), z, key=partial(customfunction.f, x))\n",
    "            y = 1 + bisect_left(range(1, 1000), z, key=lambda y: customfunction.f(x, y))\n",
    "            if customfunction.f(x, y) == z:\n",
    "                ans.append([x, y])\n",
    "                last_y = y\n",
    "\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        min_x = 1\n",
    "        max_y = 1000\n",
    "        res = []\n",
    "        while min_x < 1001 and max_y > 0:\n",
    "            ans = customfunction.f(min_x,max_y)\n",
    "            if  ans > z:\n",
    "                max_y -= 1\n",
    "            if ans == z:\n",
    "                res.append([min_x,max_y])\n",
    "                min_x += 1\n",
    "                max_y -= 1\n",
    "            if ans < z:\n",
    "                min_x += 1\n",
    "        return res\n",
    "\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        x = 1\n",
    "        y = 1000\n",
    "        res = []\n",
    "        while y>0 and x<=1000:\n",
    "            while y>0 and customfunction.f(x,y) > z:\n",
    "                y-=1 \n",
    "            if y>0 and customfunction.f(x,y) == z:\n",
    "                res.append([x,y])\n",
    "            x += 1\n",
    "        return res \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        ans = []\n",
    "        x, y = 1, 1000\n",
    "        while x <= 1000 and y:\n",
    "            res = customfunction.f(x, y)\n",
    "            if res < z:\n",
    "                x += 1\n",
    "            elif res > z:\n",
    "                y -= 1\n",
    "            else:\n",
    "                ans.append([x, y])\n",
    "                x += 1\n",
    "                y -= 1\n",
    "        return ans\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        return filter(lambda x: customfunction.f(*x) == z, itertools.product(range(1, z + 1), repeat=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        ans=[]\n",
    "        for x in range(1,1001):\n",
    "            left,right=1,1000\n",
    "            while left<=right:\n",
    "                mid=left+(right-left)//2\n",
    "                if customfunction.f(x,mid)==z:\n",
    "                    ans.append([x,mid])\n",
    "                    break\n",
    "                elif customfunction.f(x,mid)>z:\n",
    "                    right=mid-1\n",
    "                else:\n",
    "                    left=mid+1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        ans = []\n",
    "        for x in range(1,1001):\n",
    "            y = 1+bisect_left(range(1,1000),z,key=lambda y:customfunction.f(x,y))\n",
    "            if customfunction.f(x,y) == z:\n",
    "                ans.append([x,y])\n",
    "        return ans\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        ans = []\n",
    "        last_y = z + 1\n",
    "        for x in range(1, z+1):\n",
    "            left = 1\n",
    "            right = last_y\n",
    "            while right >= left:\n",
    "                y = left + right >> 1\n",
    "                s = customfunction.f(x, y)\n",
    "                if s < z:\n",
    "                    left = y + 1\n",
    "                elif s > z:\n",
    "                    right = y - 1\n",
    "                else:\n",
    "                    ans.append([x, y])\n",
    "                    last_y = y\n",
    "                    break\n",
    "\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        ans = []\n",
    "        for x in range(1, 1001):\n",
    "            left, right = 1, 1000\n",
    "            while left <= right:\n",
    "                mid = left + (right - left) // 2\n",
    "                if customfunction.f(x, mid) == z:\n",
    "                    ans.append([x, mid])\n",
    "                    break\n",
    "                elif customfunction.f(x, mid) > z:\n",
    "                    right = mid - 1\n",
    "                else:\n",
    "                    left = mid + 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, c: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        res = []\n",
    "        for x in range(1, 1001):\n",
    "            for y in range(1, 1001):\n",
    "                if c.f(x, y) == z:\n",
    "                    res.append([x, y])\n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        ans = []\n",
    "        x = 1\n",
    "        while customfunction.f(x,1) < z + 1:\n",
    "            y = 1\n",
    "            while customfunction.f(x, y) < z:\n",
    "                y += 1\n",
    "            if customfunction.f(x, y) == z:\n",
    "                ans.append([x,y])\n",
    "            x += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        lx, rx = 1, 1000\n",
    "        output = []\n",
    "        while True:\n",
    "            tx = (lx + rx) // 2\n",
    "            z1 = customfunction.f(tx, 1)\n",
    "            z2 = customfunction.f(tx + 1, 1)\n",
    "            if z1 <= z  and z2 > z:\n",
    "                break \n",
    "            else:\n",
    "                if rx - lx <= 1:\n",
    "                    break\n",
    "                if z2 <= z:\n",
    "                    lx = tx\n",
    "                if z1 > z:\n",
    "                    rx = tx\n",
    "\n",
    "        ly, ry = 1, 1000\n",
    "        for ttx in range(1, tx+1):\n",
    "            ly = 1\n",
    "            while True:\n",
    "                ty = (ly + ry) // 2\n",
    "                z1 = customfunction.f(ttx, ty)\n",
    "                if z1 == z:\n",
    "                    output.append([ttx, ty])\n",
    "                    break\n",
    "                else:\n",
    "                    if ry - ly <= 1:\n",
    "                        break\n",
    "                    if z1 < z:\n",
    "                        ly = ty\n",
    "                    if z1 > z:\n",
    "                        ry = ty\n",
    "        return output\n",
    "\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, c: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        res = []\n",
    "        y = 1000\n",
    "        for x in range(1, 1001):\n",
    "            while y and c.f(x, y) > z:\n",
    "                y -= 1\n",
    "            if y == 0:\n",
    "                break\n",
    "            if c.f(x, y) == z:\n",
    "                res.append([x, y])\n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        ans = []\n",
    "        for x in range(1, z+1):\n",
    "            left = 1\n",
    "            right = z + 1\n",
    "            while right >= left:\n",
    "                y = left + right >> 1\n",
    "                s = customfunction.f(x, y)\n",
    "                if s < z:\n",
    "                    left = y + 1\n",
    "                elif s > z:\n",
    "                    right = y - 1\n",
    "                else:\n",
    "                    ans.append([x, y])\n",
    "                    break\n",
    "\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        ans = []\n",
    "        x, y = 1, 1000\n",
    "        while x <= 1000 and y:\n",
    "            res = customfunction.f(x, y)\n",
    "            if res < z:\n",
    "                x += 1\n",
    "            elif res > z:\n",
    "                y -= 1\n",
    "            else:\n",
    "                ans.append([x, y])\n",
    "                x += 1\n",
    "                y -= 1\n",
    "        return ans\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        ans = []\n",
    "        for x in range(1, 1001):\n",
    "            if customfunction.f(x, 1) > z:\n",
    "                break\n",
    "            for y in range(1, 1001):\n",
    "                if customfunction.f(x, y) == z:\n",
    "                    ans.append([x, y])\n",
    "                elif customfunction.f(x, y) > z:\n",
    "                    break\n",
    "        return ans\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        res = []\n",
    "        for x in range(1, 1001):\n",
    "            if customfunction.f(x, 1000) < z:\n",
    "                continue\n",
    "            if customfunction.f(x, 1) > z:\n",
    "                break\n",
    "            y = 1 + bisect_left(range(1,1000), z, key=lambda i: customfunction.f(x,i))\n",
    "            if customfunction.f(x, y) == z:\n",
    "                res.append([x,y])\n",
    "        return res\n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        ans = []\n",
    "        x,y = 1,1000\n",
    "        while x <= 1000 and y >= 1:\n",
    "            l,r = 1,y\n",
    "            while l<=r:\n",
    "                mid = (l+r)>>1\n",
    "                if customfunction.f(x,mid) > z: r = mid-1\n",
    "                else: l = mid+1\n",
    "            y = r\n",
    "            if y == 0: break\n",
    "            if customfunction.f(x,y) == z: ans.append([x,y])\n",
    "            x += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\r\n",
    "   This is the custom function interface.\r\n",
    "   You should not implement it, or speculate about its implementation\r\n",
    "   class CustomFunction:\r\n",
    "       # Returns f(x, y) for any given positive integers x and y.\r\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\r\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\r\n",
    "       def f(self, x, y):\r\n",
    "  \r\n",
    "\"\"\"\r\n",
    "\r\n",
    "class Solution:\r\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\r\n",
    "        # 1.BF\r\n",
    "        ans = []\r\n",
    "        for x in range(1, 1001):\r\n",
    "            for y in range(1, 1001):\r\n",
    "                if customfunction.f(x, y) == z:\r\n",
    "                    ans.append([x, y])\r\n",
    "                elif customfunction.f(x, y) > z:\r\n",
    "                    break\r\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        ans = []\n",
    "        for x in range(1, 1001):\n",
    "            y = 1 + bisect_left(range(1, 1000), z, key=lambda y: customfunction.f(x, y))\n",
    "            if customfunction.f(x, y) == z:\n",
    "                ans.append([x, y])\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        ans = []\n",
    "        for x in range(1, 1001):\n",
    "            left, right = 1, 1000\n",
    "            while left <= right:\n",
    "                mid = left + (right - left) // 2\n",
    "                if customfunction.f(x, mid) == z:\n",
    "                    ans.append([x, mid])\n",
    "                    break\n",
    "                elif customfunction.f(x, mid) > z:\n",
    "                    right = mid - 1\n",
    "                else:\n",
    "                    left = mid + 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        res = []\n",
    "        for x in range(1,1001):\n",
    "            l = 1\n",
    "            r = 1000\n",
    "            while l < r :\n",
    "                mid = l + r >> 1\n",
    "                if customfunction.f(x,mid) == z:\n",
    "                    res.append([x,mid])\n",
    "                    break\n",
    "                elif customfunction.f(x,mid) > z:\n",
    "                    r = mid\n",
    "                else:\n",
    "                    l = mid + 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, CustomFunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        def f(x, y):\n",
    "            return CustomFunction.f(x, y)\n",
    "            \n",
    "        res = []\n",
    "        def binary_search_2d(x1, y1, x2, y2):\n",
    "            if x1 > x2 or y1 > y2:\n",
    "                return\n",
    "            if f(x1, y1) > z or f(x2, y2) < z:\n",
    "                return\n",
    "            if x1 == x2 and y1 == y2 and f(x1, y1) == z:\n",
    "                res.append([x1, y1])\n",
    "                return\n",
    "            if x1 == x2:\n",
    "                y3 = (y1 + y2) // 2\n",
    "                binary_search_2d(x1, y1, x2, y3)\n",
    "                binary_search_2d(x1, y3 + 1, x2, y2)\n",
    "                return\n",
    "            x3 = (x1 + x2) // 2\n",
    "            binary_search_2d(x1, y1, x3, y2)\n",
    "            binary_search_2d(x3 + 1, y1, x2, y2)\n",
    "        binary_search_2d(1, 1, 1000, 1000)\n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        ans = []\n",
    "        for x in range(1,1000):\n",
    "            l,r = 1, 1000\n",
    "            while l < r:\n",
    "                mid = (l+r)//2\n",
    "                if customfunction.f(x,mid) < z:\n",
    "                    l = mid + 1\n",
    "                elif customfunction.f(x,mid) > z:\n",
    "                    r = mid\n",
    "                else:\n",
    "                    ans.append([x,mid])\n",
    "                    break\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        ans = []\n",
    "        x, y = 1, 1000\n",
    "        while x <= 1000 and y:\n",
    "            res = customfunction.f(x, y)\n",
    "            if res < z:\n",
    "                x += 1\n",
    "            elif res > z:\n",
    "                y -= 1\n",
    "            else:\n",
    "                ans.append([x, y])\n",
    "                x += 1\n",
    "                y -= 1\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\r\n",
    "   This is the custom function interface.\r\n",
    "   You should not implement it, or speculate about its implementation\r\n",
    "   class CustomFunction:\r\n",
    "       # Returns f(x, y) for any given positive integers x and y.\r\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\r\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\r\n",
    "       def f(self, x, y):\r\n",
    "  \r\n",
    "\"\"\"\r\n",
    "\r\n",
    "class Solution:\r\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\r\n",
    "        answer = list()\r\n",
    "        for x in range(1, 1001):\r\n",
    "            if customfunction.f(x, 1) > z:\r\n",
    "                break\r\n",
    "            for y in range(1, 1001):\r\n",
    "                w = customfunction.f(x, y)\r\n",
    "                if w == z:\r\n",
    "                    answer.append([x, y])\r\n",
    "                    break\r\n",
    "                elif w < z:\r\n",
    "                    continue\r\n",
    "                else:\r\n",
    "                    break\r\n",
    "        return answer\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "\n",
    "        ans = []\n",
    "\n",
    "        x, y = 1, 1000\n",
    "\n",
    "        while x <= 1000 and y:\n",
    "\n",
    "            res = customfunction.f(x, y)\n",
    "\n",
    "            if res < z:\n",
    "\n",
    "                x += 1\n",
    "\n",
    "            elif res > z:\n",
    "\n",
    "                y -= 1\n",
    "\n",
    "            else:\n",
    "\n",
    "                ans.append([x, y])\n",
    "\n",
    "                x += 1\n",
    "\n",
    "                y -= 1\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        ans = []\n",
    "        left = 1\n",
    "        right = 1000\n",
    "        while left and right >= 1:\n",
    "                res = customfunction.f(left, right)\n",
    "                if res > z:\n",
    "                    right -= 1\n",
    "                elif res < z:\n",
    "                    left += 1\n",
    "                else:\n",
    "                    ans.append([left, right])\n",
    "                    left += 1\n",
    "                    right -= 1\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        ans = []\n",
    "        l, r = 1, 1000\n",
    "        while l <= 1000 and r >= 1:\n",
    "            f = customfunction.f(l, r)\n",
    "            if f > z: r -= 1\n",
    "            elif f < z: l += 1\n",
    "            else:\n",
    "                ans.append([l, r])\n",
    "                l += 1\n",
    "                r -= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        ans = []\n",
    "        j = 1000\n",
    "        for i in range(1, 1001):\n",
    "            while j > 1 and customfunction.f(i, j) > z: j -= 1\n",
    "            if customfunction.f(i, j) == z: ans.append([i, j])\n",
    "        return ans        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        res = []\n",
    "        for x in range(1, 1001):\n",
    "            for y in range(1, 1001):\n",
    "                f = customfunction.f(x, y)\n",
    "                if f == z:\n",
    "                    res.append([x, y])\n",
    "                    break\n",
    "                if f > z:\n",
    "                    break\n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        res = []\n",
    "        y = 1000\n",
    "        for x in range(1, 1001):\n",
    "            while y and customfunction.f(x, y) > z:\n",
    "                y -= 1\n",
    "            if y == 0:\n",
    "                break\n",
    "            if customfunction.f(x, y) == z:\n",
    "                res.append([x, y])\n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        \n",
    "        ans = []\n",
    "        j = 1000\n",
    "        for i in range(1, 1001):\n",
    "            while j > 1 and customfunction.f(i, j) > z: j -= 1\n",
    "            if customfunction.f(i, j) == z:\n",
    "                ans.append([i, j])\n",
    "                #if customfunction.f(j, i) ==z and i !=j :ans.append([j, i])\n",
    "        return ans        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        #try \n",
    "        ans=[]\n",
    "        for x in range(1,z+1):\n",
    "            ys=1\n",
    "            yb=z\n",
    "            while ys<=yb:\n",
    "                mid=(ys+yb)//2\n",
    "                res=customfunction.f(x,mid)\n",
    "                if res<z:\n",
    "                    ys=mid+1\n",
    "                elif res>z:\n",
    "                    yb=mid-1\n",
    "                else:\n",
    "                    ans.append([x,mid])\n",
    "                    break\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        x = 1\n",
    "        y = 1\n",
    "        reslut = []\n",
    "        re = customfunction.f(x,y)\n",
    "        while re<=z:\n",
    "            while re<=z:\n",
    "                re = customfunction.f(x,y)\n",
    "                if re==z:\n",
    "                    reslut.append([x,y])\n",
    "                y = y+1\n",
    "            x = x+1\n",
    "            y = 1\n",
    "            re = customfunction.f(x,y)\n",
    "        return reslut\n",
    "            \n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        res = []\n",
    "        for i in range(1, 1001):\n",
    "            l = 1\n",
    "            r = 1000\n",
    "            while(l < r):\n",
    "                mid = l + (r - l) // 2\n",
    "                if customfunction.f(i, mid) >= z:\n",
    "                    r = mid\n",
    "                else:\n",
    "                    l = mid + 1\n",
    "            if customfunction.f(i, l) == z:\n",
    "                res.append([i, l])\n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        res=[]\n",
    "        # def binary_search():\n",
    "\n",
    "        for i in range(1,z+1):\n",
    "            left=1\n",
    "            right=z+1\n",
    "            while left<=right:\n",
    "                j=(left+right)//2\n",
    "                if customfunction.f(i,j)>z:\n",
    "                    right=j-1\n",
    "                    j=(left+right)//2\n",
    "                elif customfunction.f(i,j)<z:\n",
    "                    left=j+1\n",
    "                    j=(left+right)//2\n",
    "                else:\n",
    "                    res.append([i,j])\n",
    "                    break\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: \"CustomFunction\", z: int) -> List[List[int]]:\n",
    "        ans = []\n",
    "        for x in range(1, 1001):\n",
    "            y = 1 + bisect_left(range(1, 1001), z, key=lambda y: customfunction.f(x, y))\n",
    "            if customfunction.f(x, y) == z:\n",
    "                ans.append([x, y])\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        ans = []\n",
    "        j = 1000\n",
    "        for i in range(1, 1001):\n",
    "            l, r = 1, 1000\n",
    "            while l <= r:\n",
    "                mid = (r - l )//2 + l\n",
    "                if customfunction.f(i, mid) == z:\n",
    "                    j = mid\n",
    "                    break\n",
    "                if customfunction.f(i, mid) < z:\n",
    "                    l = mid+1\n",
    "                else:\n",
    "                    r = mid-1      \n",
    "            if customfunction.f(i, j) == z: ans.append([i, j])\n",
    "        return ans        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "   This is the custom function interface.\n",
    "   You should not implement it, or speculate about its implementation\n",
    "   class CustomFunction:\n",
    "       # Returns f(x, y) for any given positive integers x and y.\n",
    "       # Note that f(x, y) is increasing with respect to both x and y.\n",
    "       # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n",
    "       def f(self, x, y):\n",
    "  \n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:\n",
    "        left, right = 1, 1000\n",
    "        result = list()\n",
    "        while left <= 1000 and right >= 1:\n",
    "            while right >=1 and customfunction.f(left, right) > z:\n",
    "                right -= 1\n",
    "            if right == 0:\n",
    "                break\n",
    "            if customfunction.f(left, right) == z:\n",
    "                result.append([left, right])\n",
    "            left += 1\n",
    "        return result\n",
    "        "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
